package com.tg.config;

import com.tg.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/",
                        "/css/**",
                        "/js/**",
                        "/plugins/**",
                        "/imgs/**",
                        "/icons/**",
                        "/index.html",
                        "/pages/login.html",
                        "/pages/register.html",
                        "/pages/chat.html",
                        "/users/*",
                        "/uploadImg/**",
                        "D:/JavaSpace/ideaWorkSpace/chat/src/main/resources/static/uploadImg/**"
                );
    }

    // 简写形式
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//添加映射路径，“/**”表示对所有的路径实行全局跨域访问权限的设置
                .allowedOriginPatterns("*")//开放哪些ip、端口、域名的访问权限
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")//开放哪些Http方法，允许跨域访问
                .allowedHeaders("*")//允许HTTP请求中的携带哪些Header信息
                .exposedHeaders("*") //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
                .allowCredentials(true)//是否允许发送Cookie信息
                .maxAge(3600);
    }



/*    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploadImg/**")
                .addResourceLocations("file:D:/JavaSpace/ideaWorkSpace/chat/src//main/resources/static/uploadImg/");
    }*/
}
